package com.wzy.pachong;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;

import org.apache.commons.io.FileUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

class PageParse extends Thread {

	private boolean exit = false;

	private Config mConfig;

	private LinkedList<Page> urlList;

	private String[] host;

	public PageParse(Config config) {
		mConfig = config;
		urlList = mConfig.getUrlList();
		host = config.getHost();
	}

	@Override
	public void run() {
		try {
			while (true) {
				Page page = urlList.poll();
				if (page == null) {
					sleep(100);
				} else {
					doParse(page);
				}
			}
		} catch (Exception e) {
			if (exit) {
				return;
			}
			e.printStackTrace();
		}
	}

	private void doParse(Page page) {

		try {
			if (page.getPageUrl() == null) {
				return;
			}
			Connection conn = Jsoup.connect(page.getPageUrl());
			conn.header("User-Agent", mConfig.ua);
			Document doc = conn.get();
			addTargetPage(doc);
			obtainPicData(doc, page);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addTargetPage(Document doc) {
		String[] helpRules = mConfig.getHelpRule();
		for (int m = 0; m < helpRules.length; m++) {
			Elements elements = doc.select(helpRules[m]);
			for (int i = 0; i < elements.size(); i++) {
				Element element = elements.get(i);
				if (element != null) {

					String href = element.attr("href");
					String name = element.text();
					if (name == null || "".equals(name)) {
						name = element.attr("title");
					}
					Page page = new Page(name,
							href.startsWith("http://") ? href
									: (host[m] + (href.indexOf('/') == 0 ? href
											.substring(1) : href)));
					if (urlList.indexOf(page) == -1) {
						urlList.add(page);
//						System.out.println("add page ==" + page);
					}
				}

			}
		}

	}

	public void obtainPicData(Document doc, Page page) {
		if (page.getPageName() == null || "".equals(page.getPageName())) {
			return;
		}
		Elements elements = doc.select(mConfig.getTargetRule());
		if (elements == null || elements.size() == 0) {
			return;
		}
		// System.out.println(elements);
		try {
			FileUtils.writeStringToFile(new File(mConfig.getLocalPath() ,page.getPageName()
					+ ".html"), "<html>" + elements.toString() + "</html>");
			System.out.println(page);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		File file = new File("D:/mm/");
		boolean t = file.mkdir();
		System.out.println(t);
	}
}
